﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using YArchitech.Revit;

namespace Model
{
	public class SortableEquip : IComparable
	{
		public SortableEquip(XYZ ptPos, FamilyInstance fi, double dParam, double dDist)
		{
			this.m_ptPos = ptPos;
			this.m_fiEquip = fi;
			this.m_dParam = dParam;
			this.m_dDist2Conduit = dDist;
			this.m_lstConduit = new List<Conduit>();
		}

		public int CompareTo(object obj)
		{
			SortableEquip sortableEquip = (SortableEquip)obj;
			if (sortableEquip == null)
			{
				return 0;
			}
			if (sortableEquip.m_dDist2Conduit - this.m_dDist2Conduit > SortableEquip.s_dEpsilon)
			{
				return 1;
			}
			return -1;
		}

		public FamilyInstance CreateFitting(Document doc, ConduitType type)
		{
			if (!this.IsValid)
			{
				return null;
			}
			FamilyInstance familyInstance = null;
			if (this.m_lstConduit.Count > 1)
			{
				SubTransaction subTransaction = new SubTransaction(doc);
				try
				{
					subTransaction.Start();
					switch (this.m_lstConduit.Count)
					{
					case 2:
					{
						Connector conduitConn = this.GetConduitConn(this.m_lstConduit[0]);
						Connector conduitConn2 = this.GetConduitConn(this.m_lstConduit[1]);
						if (conduitConn != null && conduitConn2 != null)
						{
							familyInstance = doc.Create.NewElbowFitting(conduitConn, conduitConn2);
						}
						break;
					}
					case 3:
					{
						Connector conduitConn3 = this.GetConduitConn(this.m_lstConduit[0]);
						Connector conduitConn4 = this.GetConduitConn(this.m_lstConduit[1]);
						Connector conduitConn5 = this.GetConduitConn(this.m_lstConduit[2]);
						if (conduitConn3 != null && conduitConn4 != null && conduitConn5 != null)
						{
							Connector connector = null;
							Connector connector2 = null;
							Connector connector3 = null;
							ElementId elementId = type.get_Parameter(BuiltInParameter.RBS_CURVETYPE_DEFAULT_TEE_PARAM).AsElementId();
							FamilySymbol familySymbol = doc.GetElement(elementId) as FamilySymbol;
							UnifiedModified.ActivateFamilySymbol(familySymbol);
							familyInstance = doc.Create.NewFamilyInstance(this.Position, familySymbol, 0);
							foreach (object obj in familyInstance.MEPModel.ConnectorManager.Connectors)
							{
								Connector connector4 = (Connector)obj;
								if (connector4.CoordinateSystem.BasisZ.IsAlmostEqualTo(XYZ.BasisZ) || connector4.CoordinateSystem.BasisZ.IsAlmostEqualTo(-XYZ.BasisZ))
								{
									connector3 = connector4;
								}
								else if (connector4.CoordinateSystem.BasisZ.IsAlmostEqualTo(XYZ.BasisX))
								{
									connector2 = connector4;
								}
								else if (connector4.CoordinateSystem.BasisZ.IsAlmostEqualTo(-XYZ.BasisX))
								{
									connector = connector4;
								}
							}
							if (connector != null && connector2 != null && connector3 != null)
							{
								Line line = (this.m_lstConduit[0].Location as LocationCurve).Curve as Line;
								double num = connector2.CoordinateSystem.BasisZ.AngleOnPlaneTo(line.Direction, XYZ.BasisZ);
								Line line2 = Line.CreateBound(this.Position, this.Position + XYZ.BasisZ * 10.0);
								ElementTransformUtils.RotateElement(doc, familyInstance.Id, line2, num);
								conduitConn3.Origin = connector.Origin;
								conduitConn4.Origin = connector2.Origin;
								conduitConn5.Origin = connector3.Origin;
								conduitConn3.ConnectTo(connector);
								conduitConn4.ConnectTo(connector2);
								conduitConn5.ConnectTo(connector3);
							}
						}
						break;
					}
					}
				}
				catch
				{
					subTransaction.RollBack();
					return null;
				}
				subTransaction.Commit();
				return familyInstance;
			}
			return familyInstance;
		}

		private Connector GetConduitConn(Conduit elemConduit)
		{
			if (elemConduit != null)
			{
				ConnectorSetIterator connectorSetIterator = elemConduit.ConnectorManager.Connectors.ForwardIterator();
				while (connectorSetIterator.MoveNext())
				{
					object obj = connectorSetIterator.Current;
					Connector connector = obj as Connector;
					if (connector != null && connector.Origin.IsAlmostEqualTo(this.Position))
					{
						return connector;
					}
				}
			}
			return null;
		}

		public XYZ Position
		{
			get
			{
				return this.m_ptPos;
			}
			set
			{
				this.m_ptPos = value;
			}
		}

		public double Elev
		{
			get
			{
				foreach (object obj in this.m_fiEquip.MEPModel.ConnectorManager.Connectors)
				{
					Connector connector = (Connector)obj;
					if (connector.Domain == (Autodesk.Revit.DB.Domain)2)
					{
						return connector.Origin.Z;
					}
				}
				return 0.0;
			}
		}

		public double Dist2Conduit
		{
			get
			{
				return this.m_dDist2Conduit;
			}
		}

		public double Dist2ConduitAbs
		{
			get
			{
				return Math.Abs(this.m_dDist2Conduit);
			}
		}

		public List<Conduit> ListConduits
		{
			get
			{
				return this.m_lstConduit;
			}
		}

		public FamilyInstance FiEquip
		{
			get
			{
				return this.m_fiEquip;
			}
		}

		public bool IsValid
		{
			get
			{
				return this.m_bValidCreateFitting;
			}
			set
			{
				this.m_bValidCreateFitting = value;
			}
		}

		private XYZ m_ptPos;

		private FamilyInstance m_fiEquip;

		private double m_dParam;

		private double m_dDist2Conduit;

		private List<Conduit> m_lstConduit;

		private bool m_bValidCreateFitting = true;

		public static double s_dEpsilon = 1E-06;
	}
}
